package tk.hongbo.biometric

import android.content.Context
import androidx.biometric.BiometricManager
import androidx.biometric.BiometricPrompt
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity


/**
 * 生物识别
 * Created by HongboZhao on 2021/10/27.
 */
object BiometricHelper {

    /**
     * 生物识别是否可用状态码
     */
    fun isFingerprintAvailable(context: Context): Int {
        val manager = BiometricManager.from(context)
        return manager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK)
    }

    fun authenticate(context: FragmentActivity, callback: BiometricPrompt.AuthenticationCallback) {
        val promptInfo = createUI()
        val prompt = BiometricPrompt(context, ContextCompat.getMainExecutor(context), callback)
        prompt.authenticate(promptInfo)
    }

    private fun createUI(): BiometricPrompt.PromptInfo {
        return BiometricPrompt.PromptInfo.Builder()
            .setTitle("注册指纹")
            .setSubtitle("安全验证指纹")
            .setNegativeButtonText("使用APP密码")
            .build()
    }
}